package com.zky.ys.test.service.impl;

import com.zky.ys.test.mapper.XueShengMapper;
import com.zky.ys.test.mapper.ZhuanYeMapper;
import com.zky.ys.test.pojo.XueSheng;
import com.zky.ys.test.pojo.XueYuan;
import com.zky.ys.test.mapper.XueYuanMapper;
import com.zky.ys.test.pojo.ZhuanYe;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.Map;

/**
 * (XueYuan)表服务实现类
 *
 * @author yangsen
 * @since 2021-06-02 11:27:46
 * @version 1.0
 */
@Service("xueYuanService")
public class XueYuanServiceImpl  {
    @Resource
    private XueYuanMapper xueYuanMapper;
    @Resource
    private ZhuanYeMapper zhuanYeMapper;
    @Resource
    private XueShengMapper xueShengMapper;


    /**
     * 新增数据
     *
     * @param xueYuan 实例对象
     * @return 实例对象
     */
    public Map<String, Object> addOne(XueYuan xueYuan) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        this.xueYuanMapper.addOne(xueYuan);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID修改单条数据
     *
     * @param xueYuan 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(XueYuan xueYuan) {
        this.xueYuanMapper.updateById(xueYuan);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(int id) {
        List<ZhuanYe> zhuanYeList = this.zhuanYeMapper.chaXunByXueYuanId(id);
        if (zhuanYeList.size() == 0){
            this.xueYuanMapper.deleteById(id);
        }else{
            for (int i = 0;i<zhuanYeList.size() ;i++){
             int zhuanYeId = zhuanYeList.get(i).getId();
                List<XueSheng> xueShengList = this.xueShengMapper.chaXunByZhuanYeId(zhuanYeId);
                if (xueShengList.size() == 0){
                    this.zhuanYeMapper.deleteByXueYuanId(id);
                    this.xueYuanMapper.deleteById(id);
                }else{
                    xueShengMapper.deleteByZhuanYeId(zhuanYeId);
                    this.zhuanYeMapper.deleteByXueYuanId(id);
                    this.xueYuanMapper.deleteById(id);
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "删除成功");
        return map;
    }
}